package com.woniuxy.service.impl;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.woniuxy.api.RemoteProductService;
import com.woniuxy.dao.OrderMapper;
import com.woniuxy.entity.Order;
import com.woniuxy.entity.Product;
import com.woniuxy.entity.ProductDTO;
import com.woniuxy.entity.utils.ResponseEntity;
import com.woniuxy.service.OrderService2;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@Service
public class OrderServiceImpl2 implements OrderService2 {

    @Autowired
    RemoteProductService remoteProductService;

    @Autowired
    OrderMapper orderMapper;

    @Override
    public ResponseEntity generateOrder(Integer pid, Integer num) {
        //使用入门的微服务调用方案
        ResponseEntity productResp = remoteProductService.findOneById(pid);
        //写业务代码
        Product product = new Product();
        //处理如果获取不到数据
        if (Integer.parseInt(productResp.get(ResponseEntity.ResponseStatus.CODE.value())+"")==HttpStatus.INTERNAL_SERVER_ERROR.value()){
            return productResp;
        }
        Map map= (Map) productResp.get(ResponseEntity.ResponseStatus.DATA.value());
        try {
            //想办法把LinkedHashMap转换为Product对象
            BeanUtils.populate(product,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        Order order = new Order();
        //用户信息假数据
        order.setUid(1);
        order.setUsername("tom");
        order.setPid(product.getPid());
        order.setPname(product.getName());
        order.setPrice(product.getPrice());
        //存储购买的数量
        order.setNumber(num);
        Order returnOrder = null;
        //如果订单入库成功，应该扣减库存
        if (product.getStock()-num>=0) {//商品扣减后必须大于等于0才能够扣减商品
            returnOrder = orderMapper.save(order);
            product.setStock(product.getStock()-num);
            return remoteProductService.updateProduct(product);
        }else{
            return new ResponseEntity(HttpStatus.NOT_MODIFIED.value(),"商品卖完了。");
        }

    }


    @Override
    //@HystrixCommand //表示该方法，受到hystrix控制
    //第二种，设置降级方法，局部参数
    //@HystrixCommand(fallbackMethod = "degradationMethod")
    public ResponseEntity findProductList(String name, double minPrice, double maxPrice) {


//        Map map = new HashMap<>();
//        map.put("name",name);
//        map.put("minPrice",minPrice);
//        map.put("maxPrice",maxPrice);
        //return remoteProductService.findAllByNameOrPrice1(Map map)
        ResponseEntity all = remoteProductService.findAllByNameOrPrice1(new ProductDTO(name, minPrice, maxPrice));

        return all;
    }

    /*public ResponseEntity degradationMethod(String name, double minPrice, double maxPrice){
        return new ResponseEntity(HttpStatus.BANDWIDTH_LIMIT_EXCEEDED.value(),"服务器正在维护中");
    }*/

}
